import { _decorator, Component, director, Node, Prefab, Tween, tween, v3, Vec3, view } from 'cc';
import Banner from '../Banner';
import { NodeUtil } from '../AshFramework/Utils/NodeUtil';
import { Panel, UIManager } from '../AshFramework/Managers/UIManager';
import { Item } from './Item';
import { PoolManager } from '../AshFramework/Managers/PoolManager';
import UI_Shaking from '../AshFramework/UI/UI_Shaking';
import { DataManager } from '../AshFramework/Managers/DataManager';
import { ZTool } from '../AshFramework/Utils/ZTool';
import { EventManager, MyEvent } from '../AshFramework/Managers/EventManager';
import { ItemData } from '../Data';

const { ccclass, property } = _decorator;

@ccclass('BattleDeckPanel')
export class BattleDeckPanel extends Component {

    Panel: Node;
    Content: Node;

    items: Node[] = [];

    protected onLoad(): void {
        this.Panel = NodeUtil.GetNode("Panel", this.node);
        this.Content = NodeUtil.GetNode("Content", this.node);
    }

    Show() {
        if (Banner.NeedShowMoreAd) {
            Banner.Instance.ShowBannerAd();
        }

        this.RefreshCards();

        this.Panel.setPosition(0, -view.getVisibleSize().height / 2 - 1125 / 2, 0);
        Tween.stopAllByTarget(this.Panel);
        tween(this.Panel).to(0.2, { position: v3(0, -view.getVisibleSize().height / 2, 0) }).call(() => { }).start();
    }

    RefreshCards() {
        this.items.forEach(e => PoolManager.PutNode(e));

        let datas = ZTool.Shuffle(DataManager.equippedItemDatas);

        for (let i = 0; i < 3; i++) {
            const data = datas[i] as ItemData;

            PoolManager.GetNode("Prefabs/UI/Item", this.Content).then((item: Node) => {
                item.setRotationFromEuler(Vec3.ZERO);
                item.getComponent(Item).InitSimpleWithCallback(data, () => {
                    //出牌
                    tween(this.Panel).to(0.2, { position: v3(0, -view.getVisibleSize().height / 2 - 1125 / 2, 0) }).call(() => {
                        EventManager.Scene.emit(MyEvent.SET_ITEM, data.Index);
                        UIManager.GetInstance().HidePanel(Panel.BattleDeckPanel);
                    }).start();
                });
                item.getComponent(UI_Shaking).Stop();
                this.items.push(item);
            });
        }

    }

}